package com.ten.order.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ten.common.core.entity.EduCourse;
import com.ten.common.core.entity.EduTeacher;
import com.ten.common.core.entity.TOrder;
import com.ten.common.core.entity.TbMember;
import com.ten.common.core.vo.Result;
import com.ten.order.config.Alipay;
import com.ten.order.dao.ByUserIdDao;
import com.ten.order.dao.CourseDao;
import com.ten.order.dao.OrderPayDao;
import com.ten.order.dao.TeacherDao;
import com.ten.order.service.PayService;
import com.ten.order.vo.AliPayBean;
import com.ten.order.vo.Pay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PayServiceImpl implements PayService {
 
    /**日志对象*/
//    private static final Logger logger = LoggerFactory.getLogger(PayServiceImpl.class);
 
    @Autowired(required = false)
    private Alipay alipay;
    @Autowired(required = false)
    private OrderPayDao orderPayDao;
    @Autowired(required = false)
    private CourseDao courseDao;
    @Autowired(required = false)
    private TeacherDao teacherDao;

    @Autowired(required = false)
    private ByUserIdDao userIdDao;
    @Override
    public Result aliPay(String courseId,String memberId) throws AlipayApiException {
        //查询是否存在该订单
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        TOrder orders = orderPayDao.selectOne(wrapper);

        //2.根据课程编号查询课程详情。并创建订单。
        EduCourse course = courseDao.selectById(courseId);
        //获取讲师信息
        String teacherId = course.getTeacherId();
        QueryWrapper<EduTeacher> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("id", teacherId);
        EduTeacher teacher = teacherDao.selectOne(wrapper2);
        //获取会员信息
        TbMember tbMember = userIdDao.selectById(memberId);

        //创建新的订单
        TOrder order = new TOrder();
        order.setOrderNo(IdUtil.getSnowflakeNextId() + "");//订单号
        order.setCourseId(courseId);
        order.setCourseTitle(course.getTitle());
        order.setCourseCover(course.getCover());
        order.setTeacherName(teacher.getName()); //课程的讲师名称--查询课程时获取。
        order.setMemberId(memberId);
        order.setNickname(tbMember.getNickname());
        order.setMobile(tbMember.getMobile());
        order.setTotalFee(course.getPrice());
        order.setPayType((byte) 1);//支付类型
        order.setStatus((byte) 0);//支付状态
        order.setIsDeleted((byte) 0);//是否删除
        order.setGmtCreate(LocalDateTime.now());
        order.setGmtModified(LocalDateTime.now());
        System.out.println(order.getOrderNo() + "++++++++++++++++++++++++++++++++++++++++++++++");

        //保存订单
        int row = orderPayDao.insert(order);

//        if (orders == null||orders!=null) {
            if (orders!=null&&orders.getStatus() != null&&orders.getStatus() == 1) {
                return new Result(5000, "该订单已支付,无需再付款", null);
            } else {
                //商户订单号
                String sb = "1";
                for (int i = 0; i < 19; i++) {
                    sb=sb+(int)(Math.random() * 10);
                }
                //订单名称
                EduCourse eduCourse = courseDao.selectById(courseId);
                AliPayBean alipayBean = new AliPayBean();
                alipayBean.setOut_trade_no(sb);
                alipayBean.setSubject(eduCourse.getTitle());
                alipayBean.setTotal_amount(eduCourse.getPrice().toString());
                // logger.info("调用支付服务接口...");
                String pay = alipay.pay(courseId, alipayBean);
                System.out.println("payyy++++++"+pay);
                return new Result(2000,"支付页面",pay);
            }

    }
}